package org.xiaoyu.txy.weixin;

import org.xiaoyu.txy.weixin.util.SSLClient;
import org.xiaoyu.txy.weixin.util.UrlFormatUtil;

import com.alibaba.fastjson.JSON;

public class BaseService {

	/**
	 * get请求
	 * @param url 请求接口的地址
	 * @param data 请求数据模型
	 * @param clazz 返回数据的类型
	 * @return
	 */
	public <T> T reqInterface(String url, Object data, Class<T> clazz){
		String fullUrl = UrlFormatUtil.fullLink(url, data);
		String reuslt = SSLClient.doGetSSL(fullUrl);	
		return JSON.parseObject(reuslt, clazz);	
	}
	
	/**
	 * post请求
	 * @param url 请求接口的地址
	 * @param accessToken token
	 * @param data
	 * @param clazz
	 * @return
	 */
	public <T> T reqInterfaceAndAccessToken(String url, String accessToken, Object data, Class<T> clazz){
		String fullUrl = url;
		if(accessToken != null && !accessToken.equals("")) {
			fullUrl = fullUrl + "?access_token=" + accessToken;
		}
		String reuslt = SSLClient.doPost(fullUrl, JSON.toJSONString(data));	
		return JSON.parseObject(reuslt, clazz);	
	}
}
